﻿using System;
using StockAccessors.Interdin.Model;
using StockModel;
using Utils;

namespace StockAccessors.Interdin.Engine
{
    internal class CfdPageReader : PageReader
    {
        #region Attributes

        private AccessorGroup _ibex35;
        private AccessorGroup _continuousMarket;
        private AccessorGroup _euroStoxx50;

        private CfdWebPage _page;

        #endregion

        #region Properties

    	public CfdWebPage Page
    	{
    		get
    		{
				return _page;
    		}
			set
			{
				if (_page != value)
				{
					_page = value;
					base.OnPropertyChanged("Page");
				}
			}
    	}

        public AccessorGroup Ibex35
        {
            get
            {
                lock (base.ListLock)
                {
                    return _ibex35;
                }
            }
            private set
            {
                lock (base.ListLock)
                {
					if (_ibex35 != value)
					{
						_ibex35 = value;
						base.OnPropertyChanged("Ibex35");
					}
                }
            }
        }

        public AccessorGroup ContinuousMarket
        {
            get
            {
                lock (base.ListLock)
                {
                    return _continuousMarket;
                }
            }
            private set
            {
                lock (base.ListLock)
                {
					if (_continuousMarket != value)
					{
						_continuousMarket = value;
						base.OnPropertyChanged("ContinuousMarket");
					}
                }
            }
        }

        public AccessorGroup EuroStoxx50
        {
            get
            {
                lock (base.ListLock)
                {
                    return _euroStoxx50;
                }
            }
            private set
            {
                lock (base.ListLock)
                {
					if (_euroStoxx50 != value)
					{
						_euroStoxx50 = value;
						base.OnPropertyChanged("EuroStoxx50");
					}
                }
            }
        }

        #endregion

        #region Public Methods

        public CfdPageReader() : base(Properties.Settings.Default.RefreshRate)
        {
        }

		public void ReadOnce()
		{
			this.Page = new CfdWebPage(base.ReadPageContents(string.Format(@"https://www.interdinfuturos.com/Broker/CFDs.asp?nc={0}", base.ContractNumber)));

			this.Ibex35 = new AccessorGroup(StockGroupName.Ibex35, base.Converter.ConvertList(_page.Ibex35Group.Cfds));
			this.ContinuousMarket = new AccessorGroup(StockGroupName.ContinuousMarket, base.Converter.ConvertList(_page.ContinuousMarketGroup.Cfds));
			this.EuroStoxx50 = new AccessorGroup(StockGroupName.EuroStoxx50, base.Converter.ConvertList(_page.EuroStoxx50Group.Cfds));			
		}

        protected override void MainMethod()
        {
            try
            {
				this.ReadOnce();
            }
            catch (Exception ex)
            {
				ExceptionLogger.LogException("Exception received while reading a Cfd Page.", ex, false);
                base.OnErrorDetected();
            }
        }

        #endregion
    }
}
